package com.baosight.xinreport.sql.write;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.schema.*;
import net.sf.jsqlparser.statement.select.*;

import java.util.ArrayList;
import java.util.List;

public class SqlWriteTest {
    public static void main(String[] args) {
        // 创建表别名
        Table table1 = new Table("table1");
        Table table2 = new Table("table2");
        table1.setAlias(new Alias("a"));
        table2.setAlias(new Alias("b"));
        FromItem fromItem = table1;
        FromItem right = table2;

        // 创建字段表达式
        Column columnA1 = new Column(new Table("a"), "a1");
        Column columnB2 = new Column(new Table("b"), "b2");
        List<SelectItem> selectItems = new ArrayList<>();
        selectItems.add(new SelectExpressionItem(columnA1));
        selectItems.add(new SelectExpressionItem(columnB2));

        // 创建关联条件
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(columnA1);
        equalsTo.setRightExpression(columnB2);

        // 创建分组条件
        GroupByElement groupByElement = new GroupByElement();
        groupByElement.addGroupByExpression(columnA1);
        groupByElement.addGroupByExpression(columnB2);



        PlainSelect plainSelect = new PlainSelect();

        plainSelect.setSelectItems(selectItems);
        Join join = new Join();
        join.setRightItem(right);
        List<Join> joins = new ArrayList<>();
        joins.add(join);
        plainSelect.setFromItem(fromItem);
        plainSelect.setJoins(joins);
        plainSelect.setGroupByElement(groupByElement);

        // 创建完整的SELECT语句
        Select selectStatement = new Select();
        selectStatement.setSelectBody(plainSelect);


        // 输出SQL语句
        System.out.println(selectStatement);
    }
}
